package com.czxy.service;

import com.czxy.config.JwtProperties;
import com.czxy.mapper.AddressMapper;
import com.czxy.pojo.Address;
import com.czxy.util.JwtUtils;
import com.czxy.vo.AddressRequest;
import com.czxy.vo.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 张青卯
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressService {

    private final JwtProperties jwtProperties;

    private final AddressMapper addressMapper;

    @Autowired
    public AddressService(JwtProperties jwtProperties, AddressMapper addressMapper) {
        this.jwtProperties = jwtProperties;
        this.addressMapper = addressMapper;
    }

    /**
     * getTheAddress 获取该用户的核心地址
     * @param request 通过请求头获取该用户信息
     * @return List<Address>
     */
    public List<Address> getTheAddress(HttpServletRequest request) {
        UserInfo userInfo = getUserInfo(request);
        if (userInfo != null){
            return addressMapper.findAll(userInfo.getId());
        }else {
            return null;
        }
    }

    /**
     * addTheAddress 添加用户新地址
     * @param result 用户新添加的地址信息
     * @param request 通过请求头获取该用户信息
     * @return 返回地址ID
     */
    public Integer addTheAddress(AddressRequest result, HttpServletRequest request) {
        UserInfo userInfo = getUserInfo(request);
        update(result);
        Address address = getAddress(result);
        if (userInfo != null){
            address.setUser_id(userInfo.getId());
        }
        addressMapper.insert(address);
        return address.getId();
    }

    /**
     * getUserInfo 公共方法，获取用户信息
     * @param request 通过请求头获取该用户信息
     * @return UserInfo
     */
    private UserInfo getUserInfo(HttpServletRequest request){
        String token = request.getHeader("authorization");
        try {
            return JwtUtils.getInfoFromToken(token, jwtProperties.getPublickey());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * refreshAddress 用户修改后的地址信息
     * @param request 用户传递过来要修改的信息
     */
    public void refreshAddress(AddressRequest request) {
        update(request);
        Address address = getAddress(request);
        addressMapper.updateByPrimaryKeySelective(address);
    }

    /**
     * getAddress 公共方法，把用户传递过来的对象copy到Address对象中
     * @param request 用户传递过来的对象
     * @return Address
     */
    private Address getAddress(AddressRequest request){
        Address address = new Address();
        BeanUtils.copyProperties(request,address);
        return address;
    }

    /**
     * update 公共方法，更新数据库
     * @param result 用户传递过来的对象
     * 该方法是业务中Isdefault这个字段中数据库中只能存在一个0所以需要该方法找到该对象并修改
     */
    private void update(AddressRequest result){
        if (result.getShr_default()==0){
            List<Address> list = addressMapper.selectAll();
            for (Address address : list) {
                if (address.getIsdefault()==0){
                    address.setIsdefault(1);
                    addressMapper.updateByPrimaryKeySelective(address);
                    break;
                }
            }
        }
    }
}
